package com.techsoul.common.security.pojo;

import com.alibaba.fastjson.annotation.JSONField;
import com.techsoul.common.core.pojo.vo.PostVo;
import com.techsoul.common.core.pojo.vo.RoleVo;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 登录用户身份权限
 *
 * @author liuen
 */
@Data
public class LoginUser implements UserDetails {
    private static final long serialVersionUID = 1L;

    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 角色集合
     */
    private List<RoleVo> roles;

    /**
     * 权限集合
     */
    private Set<String> permissions;

    /**
     * 岗位集合
     */
    private List<PostVo> posts;

    /**
     * 用户信息
     */
    private UserVo user;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public LoginUser() {
    }

    public LoginUser(UserVo user, List<RoleVo> roles, Set<String> permissions, List<PostVo> posts) {
        this.user = user;
        this.roles = roles;
        this.permissions = permissions;
        this.posts = posts;
    }

    /**
     * 获取权限集合
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = ListUtils.newArrayList();
        if (this.permissions != null) {
            authorities = this.permissions.stream()
                    .filter(permission -> StringUtils.isNotBlank(permission))
                    .map(permission -> new SimpleGrantedAuthority(permission))
                    .collect(Collectors.toList());
        }
        return authorities;
    }

    /**
     * 获取用户名
     * @return
     */
    @Override
    public String getUsername() {
        return user.getUserName();
    }

    /**
     * 获取密码
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    /**
     * 判断账户是否未过期,过期无法验证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 判断用户是否解锁,锁定的用户无法进行身份验证
     *
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 判断用户凭据(密码)是否已过期的,过期的凭据防止认证
     *
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 判断用户是否可用 ,禁用的用户不能身份验证
     *
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isEnabled() {
        return true;
    }

}
